package com.sptpc.stu.controller;

import cn.hutool.core.util.StrUtil;
import com.sptpc.stu.controller.request.SearchStuRequest;
import com.sptpc.stu.model.Student;
import com.sptpc.stu.repository.StudentRepository;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.jnlp.ClipboardService;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Controller
@RequestMapping("/stu")
public class Studentcontroller {
    @Autowired
    private StudentRepository studentRepository;


    //    @RequestMapping("/all")
//    public List<Student> getAllStudents() {
//        //mock
//        Student stu = new Student("2018001", "张三", 20);
//        Student stu2 = new Student("2018002", "李四", 20);
//        List<Student> list = new ArrayList<>();
//        list.add(stu);
//        list.add(stu2);
//
//        return list;
//    }
//
//    @GetMapping(value = "/", params = "id")
//    public Student getStudentById(
//            @RequestParam String id) {
//        //mock
//        Student stu = new Student("2018001", "张三", 20);
//        return stu;
//    }
//
//    @GetMapping("/{id}")
//    public Student getStudentByPath(
//            @PathVariable String id) {
//        //mock
//        Student stu = new Student("2018001", "张三", 20);
//        return stu;
//
//    }
//
    //json传数据库
//    @PostMapping(value = "/add",
//            consumes = MediaType.APPLICATION_JSON_VALUE)
//    public Student addNewStudent(@RequestBody Student stu) {
//        //TODO:学生信息写入数据库
//        //mock
//        Student saveStu = new Student("2018001", "张三", 20);
//        return saveStu;
//    }
// json格式传数据
@PostMapping(value = "/add",
        consumes = MediaType.APPLICATION_JSON_VALUE)
public Student addNewStudent(@RequestBody Student stu) {
    // 学生信息写入数据库
    studentRepository.save(stu);
    return stu;
}
    // 修改学生信息
    // 等价于@RequestMapping(method = RequestMethod.PUT)
    @PutMapping(value = "/update",
            consumes = MediaType.APPLICATION_JSON_VALUE)
    public Student updateStudent(@RequestBody Student stu) {
        // 通过id将数据库中原有学生信息查询出来 stu.getId()
        Optional<Student> optional =
                studentRepository.findById(stu.getId());
        if (optional.isPresent()) {
            Student student = optional.get();
            if (StrUtil.isNotBlank(stu.getName())) {
                student.setName(stu.getName());
            }
            if (stu.getMajor()!=null) {
                student.setMajor(stu.getMajor());
            }

            studentRepository.saveAndFlush(student);
            return student;
        }

        return null;
    }
//
//    //form表单传数据库
//    @PostMapping(value = "/add",
//            consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
//    public Student addNewStudent2(@RequestBody Student stu) {
//        //TODO:学生信息写入数据库
//        //mock
//
//
//        return saveStu;
//    }
//
//    //form表单传数据库
//    @PostMapping(value = "/search",
//            consumes = MediaType.APPLICATION_JSON_VALUE)
//    public Student SearchStudent(@RequestBody SearchStuRequest search) {
//        //TODO:select search-where
//        //mock
//        Student saveStu = new Student("2018001", "张三", 20);
//        return saveStu;
//    }
}